package com.hy.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.hy.entitys.hy_front_userdb.Ums_member;
import com.hy.service.IUms_memberService;
import com.hy.utils.HY_Result;
import com.hy.utils.JWTUtils;
import com.hy.utils.MapReflect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 会员 前端控制器
 *
 * @author HUAYI
 * @since 2020-12-14
 */
//@CrossOrigin
@RestController
@SuppressWarnings("ALL")
@RequestMapping("/hy-frontuser-provider/ums_member")
public class Ums_memberController {

    //注入 前台会员 业务层
    @Autowired
    private IUms_memberService iums;

    /**
     * 手机验证码注册
     * @param phone
     * @param province
     * @param city
     * @param password
     * @return
     */
    @PostMapping("/phoneCodeReg.action")
    public HY_Result phoneCodeReg(@RequestParam String phone,@RequestParam String code, @RequestParam String province, @RequestParam String city,@RequestParam String password) throws SQLException {
        return iums.phoneCodeReg(phone,code,province,city,password);
    }

    /**
     * 第三方注册
     * @param qlmap
     * @return
     */
    @PostMapping("/third_reg.action")
    public HY_Result third_Reg(@RequestBody Map<String,Object> qlmap) throws IOException, SQLException {
        return iums.third_Reg(qlmap);
    }

    /**
     * 用户名验证会员是否注册
     *
     * @param username
     * @return
     */
    @RequestMapping("/userNameVerify.action")
    public HY_Result userNameVerify(@RequestParam String username) {
        return iums.userNameVerify(username);
    }

    /**
     * 手机号验证会员是否注册
     *
     * @param phone
     * @return
     */
    @RequestMapping("/phoneVerify.action")
    public HY_Result phoneVerify(@RequestParam String phone) {
        if (!ObjectUtils.isEmpty(iums.findPhone(phone))) {
            //会员存在
            return HY_Result.SUCCESS();
        }
        //会员不存在
        return HY_Result.FAILURE();
    }

    /**
     * 手机号-密码-登录
     *
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("/phonePwdLogin.action")
    public HY_Result phonePwdLogin(@RequestParam String phone, @RequestParam String password) {
        return iums.phonePwdLogin(phone, password);
    }





    /**
     * 获取token信息
     *
     * @param token
     * @return
     */
    @PostMapping("/getToken.action")
    public HY_Result getToken(@RequestHeader String token) {
        HY_Result rs = new HY_Result();
        Map<String, Object> map = new HashMap<>();
        try {
            DecodedJWT jwt = JWTUtils.getTokenInfo(token);
            rs.setCode(100);
            rs.setMessage("验证通过!");
            Map<String, Object> jwyMap = JWTUtils.getJWYMap(token);
            //JWT信息
            map.put("jwt", jwt);
            //会员信息(Map封装)
            map.put("jwtMap", jwyMap);
            Ums_member um = MapReflect.class_mapS(jwyMap, Ums_member.class);
            //会员信息
            map.put("data", um);
            rs.setData(map);
        } catch (TokenExpiredException e) {
            rs.setCode(101);
            rs.setMessage("Token已经过期!");
        } catch (SignatureVerificationException e) {
            rs.setCode(101);
            rs.setMessage("签名错误!");
        } catch (AlgorithmMismatchException e) {
            rs.setCode(101);
            rs.setMessage("加密算法不匹配!");
        } catch (Exception e) {
            rs.setCode(101);
            rs.setMessage("无效token!--->e:" + e.getMessage());
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 验证token
     *
     * @param token
     * @return
     */
    @PostMapping("/verifyToken.action")
    public HY_Result verifyToken(@RequestHeader String token) {
        HY_Result rs = new HY_Result();
        Map<String, Object> map = new HashMap<>();
        try {
            JWTUtils.verify(token);
            rs.setCode(100);
            rs.setMessage("验证通过!");
            Map<String, Object> jwyMap = JWTUtils.getJWYMap(token);
            //会员信息(Map封装)
            map.put("jwtMap", jwyMap);
            Ums_member um = MapReflect.class_mapS(jwyMap, Ums_member.class);
            //会员信息
            map.put("data", um);
            rs.setData(map);
        } catch (TokenExpiredException e) {
            rs.setCode(101);
            rs.setMessage("Token已经过期!");
        } catch (SignatureVerificationException e) {
            rs.setCode(101);
            rs.setMessage("签名错误!");
        } catch (AlgorithmMismatchException e) {
            rs.setCode(101);
            rs.setMessage("加密算法不匹配!");
        } catch (Exception e) {
            rs.setCode(101);
            rs.setMessage("无效token!--->e:" + e.getMessage());
            e.printStackTrace();
        }
        return rs;
    }

}
